package com.king.search.mq;

import com.king.config.RabbitmqConfig;
import com.king.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RabbitListener(queues = RabbitmqConfig.es_queue)
public class PostMQMessageHandler {

    @Autowired
    SearchService searchService;

    @RabbitHandler
    public void handler(PostMQIndexMessage message){
        log.info("mq 收到一条消息： {}",message.toString());
        switch (message.getType()){
            case PostMQIndexMessage.CREATE_OR_UPDATE:
                searchService.createOrUpdateIndex(message);
                break;

            case PostMQIndexMessage.REMOVE:
                searchService.removeIndex(message);
                break;

            default:
                log.error("mq 找不到对应的消息类型！ --> {}",message.toString());
                break;
        }
    }
}
